package testcase;

import player.*;
import io.*;
import card.*;
import junit.framework.TestCase;

public class PlayerUnitTest extends TestCase {
	private Player player;

	private class CardsetStub extends Cardset {
		public CardsetStub(int max) {
			super(new Cardset(max));
		}

		@Override
		public boolean add(Card c) {
			this.cards[this.n++] = new Card('s', 'A');
			return true;
		}

		@Override
		public int count() {
			return this.n;
		}

		@Override
		public void clear() {
			this.cards = null;
			this.n = 0;
		}

		@Override
		public void remove(Cardset play) {
			this.n = 0;
		}
	}

	private class IOStub extends IOManager {

		private int ratingCount;

		public Cardset ioPlayerSelectCards(Cardset hand, String playerName) {
			Cardset play = new CardsetStub(3);
			play.add(new Card());
			return play;
		}

		public int ioPlayerLandlordRating(String playerName, Cardset hand) {
			if (ratingCount == 0) {
				ratingCount++;
				return 1;
			}
			return 3;
		}
	}

	public void setUp() {
		player = new Player("Derek", 52212006, new IOStub(),
				new CardsetStub(20), new CardsetStub(20));
	}

	public void tearDown() {
	}

	/**
	 * Test case 1: function getName
	 */
	public void testGetName() {
		assertEquals(player.getName(), "Derek");
	}

	/**
	 * Test case 2: getID
	 */
	public void testGetID() {
		assertEquals(player.getID(), 52212006);
	}

	/**
	 * Test case 3:
	 */
	public void testCount() {
		assertEquals(player.countHand(), 0);
		assertEquals(player.countPlay(), 0);
	}

	public void testAddRemoveCardToCardset() {
		player.deckDeal2Hand(new Card());
		player.deckDeal2Hand(new Card());
		assertEquals(player.countHand(), 2);
		player.clearHand();
		assertEquals(player.countHand(), 0);
	}

	public void testLandlordRating() {
		assertEquals(player.landLordRating(), 1);
		assertEquals(player.landLordRating(), 3);
	}

	public void testStartTurn() {
		Cardset temp = player.startTurn();
		assertEquals(temp.count(), 1);
	}

	public void testEndTurn() {
		player.startTurn();
		assertEquals(player.countPlay(), 1);
		player.endTurn();
		assertEquals(player.countPlay(), 0);
		player.endTurn();
		assertEquals(player.countPlay(), 0);
		player.endTurn();
		assertEquals(player.countPlay(), 0);
	}

	public void testCopyConstructor() {
		Player a = new Player("test", 2, new IOManager());
		a.deckDeal2Hand(new Card('s', 'A'));
		a.deckDeal2Hand(new Card('h', '2'));
		Player b = new Player(a);
		assertEquals(a.getName(), b.getName());
		assertEquals(a.countHand(), b.countHand());
	}
}
